<?php

use Phalcon\Mvc\Model;

class Users extends Model{
    protected $id;
    protected $username;
    protected $password;
    protected $role;

    public function initialize() {
        $this->belongsTo("role", "Roles", "id");
        $this->hasOne("id", "Blogs", "user");
    }

    public function getSource() {
		return "lb_user";
	}

    public function getId() {
        return $this->id;
    }

    public function getUsername() {
        return $this->username;
    }

    //TODO:检查
    public function setUsername($username) {
        $this->username = $username;
    }

    //TODO:检查
    public function setPassword($password) {
        $this->password = sha1($password);
    }

    public function setRole($role) {
        $this->role = $role;
    }

    public function getRole() {
        return $this->role;
    }

    public function convertToArray() {
        $result = array(
            'id'        =>  (int)$this->id,
            'username'  =>  $this->username,
            'role'      =>  $this->roles->getRoleName()
        );

        //normal user and admin can show this
        if($this->role != 0) {
            $result['blog_id'] = (int)$this->blogs->getId();
        }

        return $result;
    }
}
